function train_type() {
    var _this = this;
    var page;
    _this.index = -1;
    var items;
    _this.init = function (page_person) {
        page = page_person;
        _this.initTable();
        _this.initEvent();
    };
    //按钮事件
    _this.initEvent = function () {

        var typeId = [];
        var object = {};
        /**
         * 全选事件
         */
        $("#checkAll").click(function () {
                if (this.checked) {
                    $("#trainType_table :checkbox:not(:disabled)").prop("checked", true);

                    $("#trainType_table :checkbox:not(:disabled)").each(function () {
                        if (!object[$(this).val()]) {
                            object[$(this).val()] = 1;
                            typeId.push($(this).val());
                        }
                    });
                } else {
                    $("#trainType_table :checkbox:not(:disabled)").prop("checked", false);
                    if (typeId.length > 0) {
                        for (var a = 0; a < typeId.length; a++) {
                            $("#trainType_table :checkbox:not(:disabled)").each(function () {
                                if ($(this).val() === typeId[a]) {
                                    delete object[$(this).val()];
                                    typeId.splice(a, 1);
                                }
                            })
                        }
                    }
                }
            }
        );

        /**
         * 单选事件
         */
        $("#trainType_table").on("click", "input[type=checkbox]", function () {
            if ($(this).is(":checked")) {
                if (!object[$(this).val()]) {
                    object[$(this).val()] = 1;
                    typeId.push($(this).val());
                }
            } else {
                if (typeId.length > 0) {
                    for (var a = 0; a < typeId.length; a++) {
                        if ($(this).val() === typeId[a]) {
                            delete object[$(this).val()];
                            typeId.splice(a, 1);
                        }
                    }
                }
            }
        });
        /**
         *
         * 清空所有的选择
         */
        $("#clearSelAll").on("click", function () {
            object={};
            typeId = [];
        });



        //删除按钮
        $("#btn_del").click(function () {
           /* var id = '';
            var ids = '';
            $(":checkbox[name='ckbox']:checked").each(function () {
                ids += $(this).val() + ',';
                id = $(this).val();
            });*/
            if (typeId.length===0) {
                layer.alert("请选择要删除的方案类别");
                return;
            }
            _this.index = layer.alert('是否确定删除', {
                btn: ['确定', '取消'],
                btn1: function () {
                    ajax(appPath + "/admin/trainType/delete", {'ids': typeId.toString()}, "post");
                },
                btn2: function () {
                }
            })
        });
    }

    _this.initTable = function () {
        var list_url = appPath + "/admin/trainType/paging";
        page.init("trainType_form", list_url, "trainType_table", "trainType_page", 1, 10);
        page.goPage(1);
        page.list = function (dataList) {
            $("#all").attr("checked", false);
            var len = dataList.length;
            items = dataList;
            var inner = "", item;
            for (var i = 0; i < len; i++) {
                // 组装数据
                item = dataList[i];
                inner += '<tr>';
                inner += '<td><input type="checkbox" name="ckbox"  value="' + item.intId + '" /></td>';
                inner += '<td>' + item.varName + '</td>';
                inner += '<td>' + item.order + '</td>';
                inner += '<td>' + parseFullDate(item.operDate) + '</td>';
                inner += '<td><a href="javascript:train_type.info(\'' + item.intId + '\', \'info\');"  id="btn_all" class="a a-info">详情</a> ';
                inner += '<a href="javascript:train_type.info(\'' + item.intId + '\', \'update\');" id="" class="a a-view">修改</a></td>';
                inner += '</tr>';
            }
            return inner;
            console.log(inner)
        }
    };


    _this.search = function () {
        _this.initTable();
    };

    _this.searchAll = function () {
        $("#trainType_form").find("input[id=varName]").val("");
        _this.initTable();
    };


    //详情
    _this.info=function (id, type) {
        layer.open({
            type: 2,
            title: '方案类别'+ ('info'==type? '详情' : '修改'),
            area: ['30%', '45%'],		//弹出层大小
            scrollbar: false,				//false隐藏滑动块
            content: [appPath + '/admin/trainType/info?intId=' + id + '&type=' + type , 'yes']
        });
    }

    // 新增方案分类
    _this.add = function () {

        layer.open({
            type: 2,
            title: '方案类别新增',
            area: ['30%', '45%'],		//弹出层大小
            scrollbar: false,				//false隐藏滑动块
            content: [appPath + '/admin/trainType/add?', 'yes']
        });
    };


    _this.ajax = function (url, param, type) {
        var result;
        $.ajax({
            url: url,
            async: false,
            type: 'post',
            data: param,
            success: function (data) {
                result = data;

            }
        });
        return result;
    };
}

var train_type = new train_type();



function parseFullDate(str) {
    if (!str) return "";
    if (str.length <= 10) return "";
    return str.substring(0, 10);
}



//异步请求
function ajax(url, param, type) {
    $.ajax({
        url: url,
        async: true,
        type: 'post',
        data: param,
        success: function (data) {
            result = data;
            train_type.initTable();
            if (train_type.index != -1) {
                layer.close(train_type.index);
            }
        }
    });
}
